<?php

/**
* \brief Class for message handling.
*
*/

class CIS_Messages 
{
	private $_CI_instance;

	/**
	* \brief The class constructor
	*/
	public function __construct() 
	{
		$this->_CI_instance =& get_instance();
	}


	
	public function add_error_msg($title = 'An error occurred.', $body = '') 
	{
		$error_messages = $this->_CI_instance->phpsession->get('messages', 'error');
		if(!isset($error_messages)) 
		{
			$error_messages = array();
		}
		if(!isset($error_messages[$title])) 
		{
			$error_messages[$title] = array();
		}
		$error_messages[$title][] = $body;
		
		$this->_CI_instance->phpsession->save('messages', $error_messages, 'error');
	}

	
	public function add_success_msg($title = 'Operation successful.', $body = '') 
	{
		$success_messages = $this->_CI_instance->phpsession->get('messages', 'success');
		if(!isset($success_messages)) {
			$success_messages = array();
		}
		if(!isset($success_messages[$title])) {
			$success_messages[$title] = array();
		}
		$success_messages[$title][] = $body;
		$this->_CI_instance->phpsession->save('messages', $success_messages, 'success');
	}

	
	public function add_warning_msg($title = 'Warning', $body = '') 
	{
		$warning_messages = $this->_CI_instance->phpsession->get('messages', 'warning');
		if(!isset($warning_messages)) {
			$warning_messages = array();
		}
		if(!isset($warning_messages[$title])) {
			$warning_messages[$title] = array();
		}
		$warning_messages[$title][] = $body;
		$this->_CI_instance->phpsession->save('messages', $warning_messages, 'warning');
	}

	/**
	* \brief Function that checks whether there is an error message saved in the session.
	*
	* \return True or false whether there is an error message saved in the session
	*/
	public function has_errors() 
	{
		$error = $this->_CI_instance->phpsession->get('messages', 'error');
		return isset($error);
	}
	
	/**
	* \brief Retrieves the success messages
	*/
	public function success_message() 
	{
		$messages = $this->_CI_instance->phpsession->get('messages', 'success');
		// clear errors from session
		$this->_CI_instance->phpsession->clear('messages','success');
		
		return $messages;
	}

	/**
	* \brief Retrieves the error messages
	*/
	public function error_message() 
	{

		$messages = $this->_CI_instance->phpsession->get('messages', 'error');
		// clear errors from session
		$this->_CI_instance->phpsession->clear('messages','error');

		return $messages;
	}

	/**
	* \brief Retrieves the warning messages
	*/
	public function warning_message() 
	{
		$messages = $this->_CI_instance->phpsession->get('messages', 'warning');
		// clear errors from session
		$this->_CI_instance->phpsession->clear('messages','warning');

		return $messages;
	}

	/**
	* \brief Loads the view for both success and error messages
	*/
	public function all_messages() {
		$this->success_message();
		$this->error_message();
		$this->warning_message();
	}

}